/*
Copyright 2008  Four Roads LLC conact@4-roads.com

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either 
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public 
License along with this library.  If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Collections;
using System.Web.UI;
using System.Web.UI.WebControls;
using CommunityServer.Components;
using CommunityServer.Controls;
using System.Reflection;
using System.Reflection.Emit;
using System.ComponentModel;
using System.IO;

namespace FourRoads.Links.Controls
{
	public abstract class ResLinkSkinExTemplatedWebControl : ResLinkBaseTemplatedWebControl
	{
		protected ArrayList _ExtensionControls = new ArrayList();
		protected void BuildExtensions()
		{
			//ENumerate the applciation congfiguartion for extensions
			ResLinkConfiguration config = ResLinkConfiguration.Instance();
 
			foreach(SkinExtension ex in config.SkinExtensions)
			{
				Control ctrl = FindControl(ex.Name);

				if (ctrl != null && ctrl is PlaceHolder)
				{
					PlaceHolder ctrlPlc =  ctrl as PlaceHolder;
					
					SkinExtensionControl newControl = new SkinExtensionControl(ex);

					ctrlPlc.Controls.Add(newControl);

					_ExtensionControls.Add(newControl);
				}
			}
		}

		public override void DataBind()
		{
			base.DataBind ();

			foreach(SkinExtensionControl ctrl in _ExtensionControls)
			{
				ctrl.CurrentPost = PostData;
				ctrl.DataBind();
			}
		}
		
		protected void SaveChanges()
		{
			foreach(SkinExtensionControl ctrl in _ExtensionControls)
			{
				ctrl.CurrentPost = PostData;
				ctrl.SaveChanges();
			}
		}

		protected override void AttachChildControls()
		{
			BuildExtensions();
		}

		protected abstract Post PostData
		{
			get;
		}
	}
	/// <summary>
	/// Summary description for ResLinkInsert.
	/// </summary>
	public class ResLinkInsert : ResLinkSkinExTemplatedWebControl
	{
		#region Child Controls
			Button _InsertBtn;
			Button _CancelBtn;
			Button _UpdateBtn;

			TextBox _Title;
			ITextEditor _Description;
			TextBox _Url;
		#endregion

		protected override Post PostData
		{
			get
			{
				return EditPost;
			}
		}

		private ResLinkLink _EditPost = null;
		private ResLinkLink EditPost
		{
			get
			{
				int postID = CSContext.Current.GetIntFromQueryString("PostID" , -1);

				if (postID != -1 && _EditPost == null)
				{
					_EditPost = ResLinkLink.GetSingleLink(postID);
				}

				return _EditPost;
			}
		}

		public ResLinkInsert()
		{

		}

		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad (e);

			if (!Page.IsPostBack)
				DataBind();
		}

		protected override void AttachChildControls()
		{
			base.AttachChildControls();

			_InsertBtn = FindControl("Insert") as Button;
			_CancelBtn = FindControl("Cancel") as Button;
			_UpdateBtn = FindControl("Update") as Button;
			_Title =  FindControl("TitleEdt") as TextBox;
			_Description =  FindControl("DescriptionEdt") as ITextEditor;
			_Url =  FindControl("URLEdt") as TextBox;

			_UpdateBtn.Click += new EventHandler(_UpdateBtn_Click);
			_InsertBtn.Click +=new EventHandler(_InsertBtn_Click);
			_CancelBtn.Click += new EventHandler(_CancelBtn_Click);
		}

		public override void DataBind()
		{
			base.DataBind ();

			if (EditPost != null)
			{
				_InsertBtn.Visible = false;
				_UpdateBtn.Visible = true;

				 _Title.Text = EditPost.Subject;
				_Description.Text = EditPost.Body;
				_Url.Text = EditPost.LinkUrl;
			}
			else
			{
				_InsertBtn.Visible = true;
				_UpdateBtn.Visible = false;
			}
		}


		private void _InsertBtn_Click(object sender, EventArgs e)
		{
			if (ThisSection.SectionID < 1)
				throw new CSException(CSExceptionType.SectionNotFound);

			if (!ThisSection.EnableSuggestLink)
				throw new CSException(CSExceptionType.AccessDenied);

			ResLinkLink link = new ResLinkLink();

			link.Subject = _Title.Text;
			link.Body = _Description.Text;
			link.FormattedBody = _Description.Text;
			link.LinkUrl = _Url.Text;

			_EditPost = link;

			SaveChanges();

			link = ResLinkSection.AddLink(ThisSection , link , ThisSection.SectionID);

			RedirectPage(link);
		}

		protected void RedirectPage(ResLinkLink link)
		{
			if (link.IsApproved == false)
			{
				Modal.ClosePage(Page , "-1");
			}
			else
			{
				//For completness remove the parent section from the cache so that it apears straight away
				//I could have just added the item but I didn't want to because it may leave certain data un-init
				ResLinkSection.ClearSectionCache(CSContext.Current.SectionID);
				
				if (CSContext.Current.GetIntFromQueryString("SectionID" , -1) == -1)
					ResLinkSection.ClearSectionCache(-1);

				Modal.ClosePage(Page , link.PostID.ToString() );
			}
		}

		private void _CancelBtn_Click(object sender, EventArgs e)
		{
			Modal.ClosePage(Page , "0");
		}

		private void _UpdateBtn_Click(object sender, EventArgs e)
		{
			if (ThisSection.SectionID < 1)
				throw new CSException(CSExceptionType.SectionNotFound);

			EditPost.Subject = _Title.Text;
			EditPost.Body = _Description.Text;
			EditPost.FormattedBody = _Description.Text;
			EditPost.LinkUrl = _Url.Text;

			SaveChanges();

			ThisSection.UpdateLink(EditPost);

			RedirectPage(EditPost);
		}
	}
}
